import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../../pojo/ImPojo.dart';
import '../../../util/ImConstant.dart';

class SessionIconView extends StatelessWidget {

  final ImSession session;
  final double? iconSize;

  const SessionIconView({ required this.session, this.iconSize, super.key});

  @override
  Widget build(BuildContext context) {
    var hotMessageTotal = session.hotMessageTotal??0;
    if (hotMessageTotal > 0) {
      var stack = Stack(
        alignment: Alignment.topLeft,
        children: [
          _buildIcon(),
        ],
      );
      // hotMessageTotal = hotMessageTotal <= 0 ? 0: hotMessageTotal;
      // hotMessageTotal = hotMessageTotal >= 99 ? 99: hotMessageTotal;
      if (hotMessageTotal > 0) {
        var widget = CircleAvatar(backgroundColor: Colors.red, radius: 8,
          child: Text('${hotMessageTotal}', style: const TextStyle(color: Colors.white, fontSize: 8),),);
        stack.children.add(widget);
      }
      return stack;
    }

    return _buildIcon();
  }

  Widget _buildIcon() {
    if(session.groupCode != null) { // 群聊图标
      return ClipRRect(
        borderRadius: BorderRadius.circular(10),
        child: Image(image: const AssetImage("assets/img/head.jpg"),
          width: iconSize??ImConstant.IM_USER_ICON_SIZE,
          height: iconSize??ImConstant.IM_USER_ICON_SIZE,
        ),
      );
    } else {
      return ClipRRect(
        borderRadius: BorderRadius.circular(10),
        child: Image(image: const AssetImage("assets/img/head.jpg"),
          width: iconSize??ImConstant.IM_USER_ICON_SIZE,
          height: iconSize??ImConstant.IM_USER_ICON_SIZE,
        ),
      );
    }
  }
}